// pages/community/detail/index.js

import Toast from "../../../miniprogram_npm/@vant/weapp/toast/toast";

const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        likeIcon: "like-o",
        warnDiaShow: false,
        warnMessage: "",
        show: true,
        message: "",
        popupHeight: "height: 55%",
        cliHeight: "",
        arrowIcon: "arrow-down",

        //详情页请求结果
        plogID: "",
        name: "",
        title: "",
        postTime: "",
        detailMessage: "",
        picture: "",
        comment: [],
        likenum: null,
        hasliked: 0
    },
    /**
     * 发送请求获取帖子详情
     */
    getDetail: function (pid) {
        console.log("pid")
        console.log(pid)
        var that = this
        if (pid != "") {
            wx.request({
                url: app.globalData.url + '/plog_details/',
                data: { plogID: pid, userID: getApp().globalData.user.userId },
                header: { 'content-type': 'application/json' },
                method: 'GET',
                dataType: 'json',
                responseType: 'text',
                success: function (result) {
                    if (result.statusCode == 200) {
                        console.log("ifliked below")
                        console.log(result.data.ifLiked)
                        that.setData({
                            plogID: result.data.id,
                            name: result.data.userID,
                            picture: result.data.imagePath,
                            title: result.data.plogName,
                            postTime: result.data.creatTime,
                            detailMessage: result.data.plogContent,
                            likenum: result.data.likesNum,
                            comment: result.data.plogComment,
                            hasliked: result.data.ifLiked
                        })
                        if (result.data.ifLiked == 1) {
                            that.setData({ likeIcon: "like" })
                        }
                    } else {
                        Toast.fail("获取详情失败,状态码:" + result.statusCode)
                    }
                },
                fail: () => { Toast.fail("连接失败") },
                complete: () => { }
            });
        }
    },
    /**
     * 根据点赞情况显示心形图标
    //  */
    // setHeartIcon: function(status) {
    //     if (status == 1) {
    //         this.setData({ likeIcon: "like" })
    //     } else {
    //         this.setData({ likeIcon: "like-o" })
    //     }
    // },
    /**
     * 显示/遮挡图片
     */
    onClickArrow: function () {
        if (this.data.arrowIcon == "arrow-down") {
            this.setData({ popupHeight: "height: 60px" });
            this.setData({ arrowIcon: "arrow-up" })
        } else {
            this.setData({ popupHeight: "height:55%" });
            this.setData({
                arrowIcon: "arrow-down"
            })
        }
    },
    /**
     *  发布评论
     */
    commentPlog: function () {
        var that = this
        console.log("评论内容:" + that.data.message)
        //http://localhost:8000/comment/
        if (that.data.message != "") {
            wx.request({
                url: app.globalData.url + '/comment/',
                data: {
                    userId: getApp().globalData.user.userId,
                    plogId: that.data.plogID,
                    commentContent: that.data.message
                },
                header: { 'content-type': 'application/json' },
                method: 'POST',
                dataType: 'json',
                responseType: 'text',
                success: function (result) {
                    if (result.statusCode == 200) {
                        Toast.success("评论发布成功")
                        that.refreshComment()
                    } else {
                        console.log("result.error_type")
                        console.log(result.error_type)
                        Toast.fail("评论发布失败,请重试")
                    }
                },
                fail: () => { Toast.fail("连接失败") },
            });
        } else {
            Toast.fail("请输入评论")
        }
    },
    /**
     * 重新加载评论
     */
    refreshComment: function () {
        var that = this
        console.log("refreshComment begin" + that.data.plogID)
        wx.request({
            url: app.globalData.url + '/plog_details/',
            data: { plogID: that.data.plogID, userID: getApp().globalData.user.userId },
            header: { 'content-type': 'application/json' },
            method: 'GET',
            dataType: 'json',
            responseType: 'text',
            success: function (result) {
                if (result.statusCode == 200) {
                    that.setData({
                        comment: result.data.plogComment,
                        message: ""
                    })
                    console.log("refreshComment over")
                }
            },
        });
    },
    /**
     * 举报帖子
     */
    onWarnClick: function () {
        this.setData({ warnDiaShow: true })
        //http://localhost:8000/report/
    },
    /**
     * 取消举报
     */
    onWarnDiaClose: function () {
        this.setData({
            warnDiaShow: false,
            warnMessage: ""
        })
    },
    /**
     * 确定举报
     */
    onWarnConfirm: function () {
        var that = this
        if (that.data.warnMessage != "") {
            console.log("举报内容：" + that.data.warnMessage)
            wx.request({
                url: app.globalData.url + '/report/',
                data: {
                    userId: getApp().globalData.user.userId,
                    plogId: that.data.plogID,
                    reportContent: that.data.warnMessage
                },
                header: { 'content-type': 'application/json' },
                method: 'POST',
                dataType: 'json',
                responseType: 'text',
                success: function (result) {
                    if (result.statusCode == 200) {
                        Toast.success("已举报")
                    } else {
                        console.log("result.error_type:")
                        console.log(result.error_type)
                        Toast.fail("举报失败,请重试")
                    }
                },
                fail: () => { Toast.fail("连接失败") },
            });
        } else {
            Toast.fail("请输入原因")
        }
    },
    /**
     * 点赞帖子
     */
    onLikeClick: function () {
        var that = this
        if (that.data.likeIcon != "like") {
            //http://localhost:8000/like/
            wx.request({
                url: app.globalData.url + '/like/',
                data: {
                    user_id: getApp().globalData.user.userId,
                    plog_id: that.data.plogID
                },
                header: { 'content-type': 'application/json' },
                method: 'POST',
                dataType: 'json',
                responseType: 'text',
                success: function (result) {
                    if (result.statusCode == 200) {
                        Toast.success("点赞成功！")
                        that.refreshLike()
                    } else {
                        Toast.fail("点赞失败，请重试")
                    }
                },
                fail: () => { Toast.fail("连接失败") },
                complete: () => { }
            });
        } else {
            Toast("帖子虽好,请不要重复点赞噢")
        }
    },
    /**
     * 重新加载点赞数
     */
    refreshLike: function () {
        var that = this
        console.log("refreshLike begin: pid " + that.data.plogID)
        wx.request({
            url: app.globalData.url + '/plog_details/',
            data: { plogID: that.data.plogID, userID: getApp().globalData.user.userId },
            header: { 'content-type': 'application/json' },
            method: 'GET',
            dataType: 'json',
            responseType: 'text',
            success: function (result) {
                if (result.statusCode == 200) {
                    that.setData({
                        likenum: result.data.likesNum,
                        hasliked: result.data.ifLiked
                    })
                    if (result.data.ifLiked == 1) {
                        that.setData({ likeIcon: "like" })
                    } else {
                        that.setData({ likeIcon: "like-o" })
                    }
                    console.log("refreshLike over")
                }
            },
        });
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let that = this
        that.setData({
            cliHeight: wx.getSystemInfoSync().windowHeight,
        })
        console.log(options)
        that.setData({
            plogID: options.id,
        })
        that.getDetail(options.id)
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () { },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})